import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

class DialogUtil {
  static const String succeedTag = 'succeedTag';
  static const String failTag = 'failTag';
  static const String errorTag = 'errorTag';
  static const String loadingTag = 'loadingTag';

  static void showNetworkSucceed({int second = 1}) {
    showNetworkCustomDialog(
      tag: succeedTag,
      msg: '操作成功',
      icon: Icons.check_circle_outline,
      showSeconds: second,
    );
  }

  static void showNetworkFail({int second = 1}) {
    showNetworkCustomDialog(
      tag: failTag,
      msg: '操作失败',
      icon: Icons.highlight_off,
      showSeconds: second,
    );
  }

  static void showNetworkError({int second = 1}) {
    showNetworkCustomDialog(
      tag: errorTag,
      msg: '网络异常',
      icon: Icons.sentiment_very_dissatisfied,
      showSeconds: second,
    );
  }

  static void showNetworkLoading({String? msg}) {
    showNetworkCustomDialog(tag: loadingTag, msg: msg ?? "加载中");
  }

  static void showNetworkCustomDialog({
    String? tag,
    String msg = '加载中',
    IconData? icon,
    int showSeconds = 0,
  }) {
    SmartDialog.show(
      tag: tag,
      displayTime: showSeconds > 0 ? Duration(seconds: showSeconds) : null,
      maskColor: Colors.transparent,
      builder: (context) => _builderNetworkStateWidget(msg: msg, icon: icon),
      keepSingle: true,
      bindPage: true,
    );
  }

  static void dismiss({
    String tag = loadingTag,
  }) {
    SmartDialog.dismiss(tag: tag);
  }

  static Widget _builderNetworkStateWidget({
    String msg = '加载中',
    IconData? icon,
  }) {
    return Container(
      width: 248.w,
      height: 248.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: const Color(0XFF000000).withOpacity(0.7),
        borderRadius: BorderRadius.circular(24.w),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          icon == null
              ? Image.asset(
                  "assets/images/net_loading_icon.png",
                  width: 96.w,
                  height: 96.w,
                )
              : Icon(
                  icon,
                  size: 96.w,
                  color: Colors.white,
                ),
          Padding(
            padding: EdgeInsets.only(top: 28.h),
            child: Text(
              msg,
              style: TextStyle(
                fontSize: 28.sp,
                color: Colors.white,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

enum NetworkState {
  succeed,
  fail,
  error,
  loading,
}
